import {IModel} from "./IModel";
/**
 * Created by ybjaa on 2017/6/7.
 */

export class ArrayModel<T extends IModel>{

    protected _dataSource:T[]=new Array<T>();


    get Datasource():T[]
    {
        if(!this._dataSource) return [];
        return this._dataSource;
    }

    set Datasource(value:T[]) {

        let parent=this;

        let newData = function (array: T[]) {
            if (array && array.length > 0) {
                return array.map((m) => {
                    let model:T =parent.create();
                    model.copyFrom(m);
                    return model;
                })
            }
        };

        if(!value||value.length<=0) {
            this._dataSource=[];
        } else {
            this._dataSource = newData(value);
        }

    }

    constructor(private c:{new():T;}){

    }


    protected create():T{
        return new this.c();
    }

}

